//
//  PEDeviceStatusCell.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2023/12/5.
//

#import "RHLocalEnergyStorageStatusCell.h"
#import "RHBatteryView.h"

@interface RHLocalEnergyStorageStatusCell()
@property (nonatomic, strong) RHBatteryView *batteryView;

@property (nonatomic, strong) QMUILabel *timeTitleLab;

@property (nonatomic, strong) QMUILabel *statusLab;

@property (nonatomic, strong) QMUILabel *socLab;

@property (nonatomic, strong) QMUILabel *timeLab;
@end
@implementation RHLocalEnergyStorageStatusCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic
{
    _dic = dic;
    NSInteger timeInterval = 0;
    //1.工作 2.待机 3.停机
    if([dic[@"Ems_BatPwr"] doubleValue] == 0){
        self.statusLab.text = @"静置".rh_localized;
        self.timeTitleLab.text = @"";
    }else if ([dic[@"Ems_BatPwr"] doubleValue] > 0){
        self.statusLab.text = @"充电".rh_localized;
        timeInterval = [dic[@"Ems_ChgTim"] integerValue];
       self.timeTitleLab.text = [NSString stringWithFormat:@"%@:",@"备电时长".rh_localized];
    }else if ([dic[@"Ems_BatPwr"] doubleValue] < 0){
        self.statusLab.text = @"放电".rh_localized;
        timeInterval = [dic[@"Ems_DischgTim"] integerValue];
        self.timeTitleLab.text = [NSString stringWithFormat:@"%@:",@"剩余放电时长".rh_localized];
    }
    self.socLab.text = [NSString stringWithFormat:@"%ld%%",[dic[@"Ems_BatSoc"] integerValue]];
    self.batteryView.soc = [dic[@"Ems_BatSoc"] integerValue]/100.0f;

 
    NSInteger minute = timeInterval % 60;
    NSInteger hour = timeInterval / 60;
   
    self.timeLab.text = [NSString stringWithFormat:@"%ld%@%ld%@",hour,@"小时".rh_localized,minute,@"分".rh_localized];
    if (timeInterval == 0) {
        self.timeLab.text = @"--";
    }
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:CFTabViewBgColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    [bgView setBackgroundColor:UIColor.whiteColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,15, 0, 15));
    }];
    
    
//    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"device_storege"]];
//    [bgView addSubview:iconImg];
//    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.offset(45);
//        make.left.mas_offset(15);
//        make.centerY.mas_offset(0);
//    }];
    
    _batteryView = [[RHBatteryView alloc] init];
    [bgView addSubview:_batteryView];
    [_batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(33, 18)));
        make.top.mas_offset(15);
        make.left.mas_offset(15);
    }];
    
    QMUILabel *statusTitleLab = [[QMUILabel alloc] init];
    statusTitleLab.text = [NSString stringWithFormat:@"%@:",@"状态".rh_localized];
    [statusTitleLab setTextAlignment:NSTextAlignmentLeft];
    [statusTitleLab setFont:[UIFont systemFontOfSize:14]];
    [statusTitleLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:statusTitleLab];
    [statusTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.batteryView.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.batteryView);
    }];
    
    _statusLab = [[QMUILabel alloc] init];
    _statusLab.text = @"待机".rh_localized;
    [_statusLab setTextAlignment:NSTextAlignmentLeft];
    [_statusLab setFont:[UIFont systemFontOfSize:14]];
    [_statusLab setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_statusLab];
    [_statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(statusTitleLab.mas_right).offset(2);
        make.centerY.equalTo(statusTitleLab);
    }];
    
    _socLab = [[QMUILabel alloc] init];
    _socLab.text = @"--";
    [_socLab setTextAlignment:NSTextAlignmentRight];
    [_socLab setFont:kSYSTEMFONT(12)];
    [_socLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    [bgView addSubview:_socLab];
    [_socLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.batteryView);
        make.top.equalTo(weakSelf.batteryView.mas_bottom).offset(10);
        make.bottom.mas_offset(-15);
    }];
    
    QMUILabel *timeTitleLab = [[QMUILabel alloc] init];
    timeTitleLab.text = @"--";
    [timeTitleLab setTextAlignment:NSTextAlignmentLeft];
    [timeTitleLab setFont:kSYSTEMFONT(12)];
    [timeTitleLab setTextColor:[UIColor colorWithHexString:@"3F4443" alpha:0.6]];
    [bgView addSubview:timeTitleLab];
    self.timeTitleLab = timeTitleLab;
    [timeTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.socLab.mas_right).offset(5);
        make.centerY.equalTo(weakSelf.socLab);
    }];
    
    _timeLab = [[QMUILabel alloc] init];
    _timeLab.text = @"--";
    [_timeLab setTextAlignment:NSTextAlignmentRight];
    [_timeLab setFont:kSYSTEMFONT(12)];
    [_timeLab setTextColor:kCOLOR_HEX(@"79E75A")];
    [bgView addSubview:_timeLab];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeTitleLab.mas_right).offset(5);
        make.centerY.equalTo(timeTitleLab);
    }];
    
    UIImageView *arrowImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow"]];
    [bgView addSubview:arrowImg];
    [arrowImg   mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(@(CGSizeMake(20, 20)));
        make.centerY.mas_offset(0);
        make.right.mas_offset(-15);
    }];
    
   
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
    [bgView addGestureRecognizer:tap];
    [tap.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        if(weakSelf.detailBlock){
            weakSelf.detailBlock();
        }
    }];
}


@end
